package com.example.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.common.AutoLog;
import com.example.common.JwtTokenUtils;
import com.example.common.Result;
import com.example.entity.Admin;
import com.example.entity.Grades;
import com.example.entity.Paper;
import com.example.entity.Params;
import com.example.exception.CustomException;
import com.example.service.GradesService;
import com.example.service.PaperService;
import com.github.pagehelper.PageInfo;
import dev.langchain4j.agent.tool.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/grades")
public class GradesController {
    @Resource
    private GradesService gradesService;

    @GetMapping("/search")
    public Result findBySearch(Params params){
        PageInfo<Grades> info = gradesService.findBySearch(params);
        return Result.success(info);
    }
    @GetMapping("/searchGrades")
    public Result findBySearch2(Params params){
        PageInfo<Grades> info = gradesService.findBySearch2(params);
        return Result.success(info);
    }

    @PostMapping
    @AutoLog("编辑成绩")
    public Result save(@RequestBody Grades grades){
        if(grades.getId()==null) {
            gradesService.add(grades);
        }else{
            gradesService.update(grades);
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @AutoLog("删除成绩")
    public Result delete(@PathVariable Integer id){
        gradesService.delete(id);
        return Result.success();
    }

    @PutMapping("/delBatch")
    @AutoLog("删除成绩")
    public Result delBatch(@RequestBody List<Grades> list){
        for(Grades grades : list){
            gradesService.delete(grades.getId());
        }
        return Result.success();
    }

    @GetMapping("/echarts/rose")
    public Result rose(Params params){
        List<Grades> list = gradesService.findBySearch3(params);
        List<Map<String, Object>> maplist = new ArrayList<>();
        for (Grades grades : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", grades.getNum_students());
            map.put("name", grades.getGrade_range());
            maplist.add(map);
        }

        return Result.success(maplist);
    }

    @GetMapping("/echarts/bie")
    public Result bie(){
        List<Grades> list = gradesService.findGrade();
        List<Map<String, Object>> maplist = new ArrayList<>();
        for (Grades grades : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", grades.getGrade_range());
            map.put("value", grades.getNum_students());
            maplist.add(map);
        }

        return Result.success(maplist);
    }

    @GetMapping("/echarts/bar")
    public Result bar(){
        List<Grades> list = gradesService.findGrades();
        List<String> xAxisData = new ArrayList<>();
        List<String> yAxisData = new ArrayList<>();

        for (Grades grades : list) {
            xAxisData.add(grades.getGrade_range());
            yAxisData.add(grades.getNum_students());
        }

        Map<String, Object> map = new HashMap<>();
        map.put("xAxis", xAxisData);
        map.put("yAxis", yAxisData);
        return Result.success(map);
    }

    @GetMapping("/export")
    @AutoLog("导出成绩")
    public Result export(HttpServletResponse response) throws IOException {
        List<Grades> all = gradesService.findAll2();

        if(CollectionUtil.isEmpty(all)){
            throw new CustomException("无数据！");
        }

        List<Map<String,Object>> list = new ArrayList<>();

        for(Grades grades: all){
            Map<String,Object> row = new HashMap<>();
            row.put("用户名称",grades.getUserName());
            row.put("年级",grades.getGrade());
            row.put("班级",grades.getClasses());
            row.put("成绩",grades.getGrades());
            list.add(row);
        }

        ExcelWriter wr = ExcelUtil.getWriter(true);

        wr.write(list,true);

        //下载excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=zcy/grades.xlsx");
        ServletOutputStream out = response.getOutputStream();
        wr.flush(out,true);
        wr.close();
        IoUtil.close(System.out);

        return Result.success();
    }
}
